﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EnvDTE;
using System.Web.Configuration;
using System.Reflection;
using System.Web.UI.Design;

namespace Catalyst.Web.DynamicData.Design
{
    public class WebConfigFile
    {
        private IWebApplication _webApplication;

        public WebConfigFile(IWebApplication webApplication)
        {
            _webApplication = webApplication;
        }

        public void AddAssembly()
        {   
            var config = _webApplication.OpenWebConfiguration(false);
            PagesSection section = config.GetSection("system.web/pages") as PagesSection;
            TagPrefixCollection controls = section.Controls;
            foreach (TagPrefixInfo tp in controls)
            {
                if (tp.Namespace == "Catalyst.Web.DynamicData")
                    return;
            }

            section.Controls.Add(new TagPrefixInfo("asp", "Catalyst.Web.DynamicData", GetAssemblyFullName(), null, null));
            config.Save();
        }

        private string GetAssemblyFullName()
        {
            return Assembly.GetAssembly(typeof(Catalyst.Web.DynamicData.DynamicFilterControl)).FullName;
        }
    }
}
